package org.lzy.shop.view

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.WindowManager
import org.lzy.shop.R
import org.lzy.shop.databinding.DialogCityPickerBinding
import org.lzy.shop.util.CityDataManager

/**
 * 自定义城市选择对话框
 */
class CityPickerDialog(
    context: Context,
    private val onCitySelected: (province: String, city: String, district: String) -> Unit
) : Dialog(context, R.style.BottomDialogStyle) {

    private lateinit var binding: DialogCityPickerBinding
    private var selectedProvince: String = ""
    private var selectedCity: String = ""
    private var selectedDistrict: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DialogCityPickerBinding.inflate(LayoutInflater.from(context))
        setContentView(binding.root)

        // 设置对话框样式
        val window = window
        window?.let {
            val params = it.attributes
            params.gravity = Gravity.BOTTOM
            params.width = WindowManager.LayoutParams.MATCH_PARENT
            params.height = WindowManager.LayoutParams.WRAP_CONTENT
            it.attributes = params
        }

        // 初始化数据
        initData()

        // 设置监听器
        initListeners()
    }

    private fun initData() {
        // 设置省份数据
        binding.provinceWheelView.setDataList(CityDataManager.provinces)

        // 获取默认选中的省份
        val defaultProvincePos = CityDataManager.getDefaultProvincePosition()
        binding.provinceWheelView.setSelectedPosition(defaultProvincePos)
        selectedProvince = CityDataManager.provinces[defaultProvincePos]

        // 更新城市数据
        updateCityData(selectedProvince)
    }

    private fun updateCityData(province: String) {
        val cities = CityDataManager.getCitiesByProvince(province)
        binding.cityWheelView.setDataList(cities)

        val defaultCityPos = CityDataManager.getDefaultCityPosition(province)
        binding.cityWheelView.setSelectedPosition(defaultCityPos)
        selectedCity = cities[defaultCityPos]

        // 更新区县数据
        updateDistrictData(province, selectedCity)
    }

    private fun updateDistrictData(province: String, city: String) {
        val districts = CityDataManager.getDistrictsByProvinceAndCity(province, city)
        binding.districtWheelView.setDataList(districts)

        val defaultDistrictPos = CityDataManager.getDefaultDistrictPosition(province, city)
        binding.districtWheelView.setSelectedPosition(defaultDistrictPos)
        selectedDistrict = districts[defaultDistrictPos]
    }

    private fun initListeners() {
        // 省份选择监听器
        binding.provinceWheelView.setOnItemSelectedListener { _, item ->
            selectedProvince = item
            updateCityData(item)
        }

        // 城市选择监听器
        binding.cityWheelView.setOnItemSelectedListener { _, item ->
            selectedCity = item
            updateDistrictData(selectedProvince, item)
        }

        // 区县选择监听器
        binding.districtWheelView.setOnItemSelectedListener { _, item ->
            selectedDistrict = item
        }

        // 取消按钮
        binding.btnCancel.setOnClickListener {
            dismiss()
        }

        // 确定按钮
        binding.btnConfirm.setOnClickListener {
            onCitySelected(selectedProvince, selectedCity, selectedDistrict)
            dismiss()
        }
    }
}